package com.gec.obwiki.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.obwiki.domain.dto.EbookQueryReq;
import com.gec.obwiki.domain.pojo.Ebook;
import com.gec.obwiki.domain.vo.EbookQueryResp;
import com.gec.obwiki.domain.vo.PageResp;
import com.gec.obwiki.rep.EbookSaveReq;
import com.gec.obwiki.mapper.EbookMapper;
import com.gec.obwiki.service.IEbookService;
import com.gec.obwiki.utils.CopyUtil;
import com.gec.obwiki.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author adim
* @description 针对表【ebook(电子书)】的数据库操作Service实现
* @createDate 2025-06-23 11:12:38
*/
@Service
public class EbookServiceImpl extends ServiceImpl<EbookMapper, Ebook> implements IEbookService {


    @Autowired
    private SnowFlake snowFlake;

    @Override
    public void save(EbookSaveReq req) {
        Ebook ebook = CopyUtil.copy(req, Ebook.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            // 雪花算法生成id
            long id = snowFlake.nextId();
            ebook.setId(id);
            this.baseMapper.insert(ebook);
        } else {
            // 更新
            this.baseMapper.updateById(ebook);
        }
    }

    @Override
    public List<EbookQueryResp> listByname(EbookQueryReq req) {

        QueryWrapper<Ebook> queryWrapper = new QueryWrapper<Ebook>();
//第一个参数：该参数是一个布尔类型，只有该参数是true时，才将like条件拼接到sql中；本例中，如果name字段不为空，则拼接name字段的like查询条件；
        queryWrapper.like(StringUtils.isNotBlank(req.getName()),"name",req.getName());
        queryWrapper.eq(ObjectUtils.isNotEmpty(req.getCategoryId2()),"category2_id",req.getCategoryId2());
        List<Ebook> ebooks = this.baseMapper.selectList(queryWrapper);

        List<EbookQueryResp> resps = CopyUtil.copyList(ebooks, EbookQueryResp.class);

        return resps;
}

    public void delete(Long id) {
        this.baseMapper.deleteById(id);
    }
}




